﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Animation;

namespace Comarch.Animation
{
    public class EmphasisEffects
    {
        public StoryboardBuilder StoryboardBuilder { get; private set; }
        
        internal protected EmphasisEffects(StoryboardBuilder storyboardBuilder)
        {
            StoryboardBuilder = storyboardBuilder;
        }

        public StoryboardBuilder ZoomInAndBack(FrameworkElement element, int duration)
        {
            return StoryboardBuilder
                .Scale(element, 1.2, 1.2, duration / 2, new PowerEase { EasingMode = EasingMode.EaseOut })
                .SeekToEnd()
                .Scale(element, 1, 1, duration / 2, new PowerEase { EasingMode = EasingMode.EaseIn });

        }

        public  StoryboardBuilder SmallShake(FrameworkElement element, int duration = 400, int firstAngle = 11, int secondAngle = 4)
        {
            StoryboardBuilder.RenderTransformOrigin = new Point(0.5, 0.5);
            var singleTime = duration / 5;

            return StoryboardBuilder
                .Rotate(element, firstAngle, singleTime).SeekToEnd().Rotate(element, -2 * firstAngle, singleTime).SeekToEnd()
                .Rotate(element, firstAngle + secondAngle, singleTime).SeekToEnd().Rotate(element, -secondAngle, singleTime).SeekToEnd()
                .Rotate(element, 0, singleTime);

        }

        public StoryboardBuilder Shake(FrameworkElement element, int duration = 300)
        {
            var point = element.TransformToVisual(element.FindRoot<Panel>()).Transform(new Point());
            var r = new Random();
            const int stepDuration = 50;
            for (var i = 0; i < duration; i += stepDuration)
            {
                var x2 = (int)(point.X + r.Next(-10, 10));
                var y2 = (int)(point.Y + r.Next(-10, 10));

                StoryboardBuilder.AbsoluteTranslate(element, x2, y2, stepDuration)
                    .Seek(stepDuration);
            }

            StoryboardBuilder.AbsoluteTranslate(element, (int)point.X, (int)point.Y, stepDuration);
            return StoryboardBuilder;
        }
    }
}